package com.douwong.student.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.db.PrepareReaourceDBService;
import com.douwong.student.model.PrepareResource;
import com.douwong.student.utils.FileUtils;
import com.douwong.student.utils.JsonUtils;
import com.douwong.student.utils.LogUtils;

import java.util.ArrayList;

@SuppressLint("DefaultLocale")
public class PrepareResourceAdapter extends BaseAdapter {

	private static final String TAG = PrepareResourceAdapter.class.getSimpleName();
	private Context context;
	private int i;
	private ArrayList<PrepareResource> list;

	public PrepareResourceAdapter(Context context, int i,
								  ArrayList<PrepareResource> list) {
		this.context = context;
		this.i = i;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		view = LayoutInflater.from(context).inflate(
				R.layout.class_share_item, null);
		LinearLayout done_ll = (LinearLayout) view.findViewById(R.id.done_ll);
		final TextView load_text = (TextView) view.findViewById(R.id.load_text);
		ImageView file_icon_img = (ImageView) view
				.findViewById(R.id.file_icon_img);
		TextView file_name_text = (TextView) view
				.findViewById(R.id.file_name_text);
		TextView file_size_text = (TextView) view
				.findViewById(R.id.file_size_text);
		TextView upload_user_text = (TextView) view
				.findViewById(R.id.upload_user_text);
		TextView create_time_text = (TextView) view
				.findViewById(R.id.create_time_text);
		if (i == 1) {// 资源
//			
		} else if (i == 3) {// 已下载
			done_ll.setVisibility(View.VISIBLE);
			load_text.setVisibility(View.GONE);
		}
		if (list.size() > 0) {
			create_time_text.setText(list.get(position).getCreatetime());
			file_icon_img.setImageResource(getBackgroundByName(list.get(
					position).getAttaSuffix()));


			if (list.get(position).getMcTitle()!=null){
				file_name_text.setText(list.get(position).getMcTitle());
			}else {
				file_name_text.setText(list.get(position).getFileName());
			}

			//	file_name_text.setText(list.get(position).getFileName());



			file_size_text.setText(FileUtils.formatFileSize(list.get(
					position).getFileSize()));
			LogUtils.i(TAG,list.get(position).getSourceType()+"");
			upload_user_text.setText(list.get(position).getSubjectName()
					+ "\t" + list.get(position).getUserName());
//			final TextView load_view = holder.load_text;
			if (i == 1) {
				load_text.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View paramView) {
						list.get(position).setLoading(true);
						list.get(position).setFilePath(FileUtils.RESOURCE_DIR
							+ list.get(position).getFileName());
						load_text.setText("下载中");
						load_text.setClickable(false);
						Intent intent = new Intent();
						intent.setAction(CommonConfig.GET_RESOURCE_LOAD);
						intent.putExtra("resource",
								JsonUtils.serialize(list.get(position)));
						context.sendBroadcast(intent);
						PrepareReaourceDBService.getInstance(context).upDate(
								list.get(position));
						// ClassShareAdapter.this.notifyDataSetChanged();
						// }
					}
				});
				if (list.get(position).isLoading()) {
//					Log.e("position",position+"");
//					Log.e("position",position+""+list.get(position).isLoading());
					load_text.setText("下载中");
					load_text.setClickable(false);
				} else if (list.get(position).isLoaded()) {
					load_text.setVisibility(View.GONE);
					done_ll.setVisibility(View.VISIBLE);
				}else {
					load_text.setText("下载");
					load_text.setClickable(true);
				}
			}
		}
		
//		Holder holder = null;
//		if (view == null) {
//			holder = new Holder();
//			view = LayoutInflater.from(context).inflate(
//					R.layout.class_share_item, null);
//			holder.done_ll = (LinearLayout) view.findViewById(R.id.done_ll);
//			holder.load_text = (TextView) view.findViewById(R.id.load_text);
//			if (i == 1) {// 资源
//				
//			} else if (i == 3) {// 已下载
//				holder.done_ll.setVisibility(View.VISIBLE);
//				holder.load_text.setVisibility(View.GONE);
//			}
//
//			holder.file_icon_img = (ImageView) view
//					.findViewById(R.id.file_icon_img);
//			holder.file_name_text = (TextView) view
//					.findViewById(R.id.file_name_text);
//			holder.file_size_text = (TextView) view
//					.findViewById(R.id.file_size_text);
//			holder.upload_user_text = (TextView) view
//					.findViewById(R.id.upload_user_text);
//			holder.create_time_text = (TextView) view
//					.findViewById(R.id.create_time_text);
//			view.setTag(holder);
//		} else {
//			holder = (Holder) view.getTag();
//		}
//		
//		if (list.size() > 0) {
//			holder.create_time_text.setText(list.get(position).getCreatetime());
//			holder.file_icon_img.setImageResource(getBackgroundByName(list.get(
//					position).getAttaSuffix()));
//			holder.file_name_text.setText(list.get(position).getFileName());
//			holder.file_size_text.setText(FileUtils.formatFileSize(list.get(
//					position).getFileSize()));
//			holder.upload_user_text.setText(list.get(position).getSubjectName()
//					+ "\t" + list.get(position).getUserName());
//			final TextView load_view = holder.load_text;
//			if (i == 1) {
//				holder.load_text.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View paramView) {
//						list.get(position).setLoading(true);
//						load_view.setText("下载中");
//						load_view.setClickable(false);
//						Intent intent = new Intent();
//						intent.setAction(CommonConfig.GET_RESOURCE_LOAD);
//						intent.putExtra("resource",
//								JsonUtils.serialize(list.get(position)));
//						context.sendBroadcast(intent);
//						PrepareReaourceDBService.getInstance(context).upDate(
//								list.get(position));
//						// ClassShareAdapter.this.notifyDataSetChanged();
//						// }
//					}
//				});
//				if (list.get(position).isLoading()) {
//					Log.e("position",position+"");
//					Log.e("position",position+""+list.get(position).isLoading());
//					holder.load_text.setText("下载中");
//					holder.load_text.setClickable(false);
//				} else if (list.get(position).isLoaded()) {
//					holder.load_text.setVisibility(View.GONE);
//					holder.done_ll.setVisibility(View.VISIBLE);
//				}else {
//					holder.load_text.setText("下载");
//					holder.load_text.setClickable(true);
//				}
//			}
//		}
		return view;
	}

	class Holder {
		TextView load_text, file_name_text, file_size_text, upload_user_text,
				create_time_text;
		LinearLayout done_ll;
		ImageView file_icon_img;
	}

	private int getBackgroundByName(String fileName) {
		fileName = fileName.toLowerCase();
		if (fileName.equals("jpg") || fileName.equals("png")) {
			return R.mipmap.file_pic;
		} else if (fileName.equals("mp4") || fileName.equals("avi")||fileName.equals("flv")) {
			return R.mipmap.file_video;
		} else if (fileName.equals("mp3")) {
			return R.mipmap.class_share_music;
		} else {
			return R.mipmap.class_share_file;
		}
	}

	private interface UpdateTextView{
		void update(int i, TextView textview);
	}
}
